Tutorial 6: Visualizations

R Bootcamp HTML Slides

Jared Knowles

Overview

In this lesson we hope to learn: - How to draw diagnostic plots in base graphics - Colors - `ggplot2’ - Basic geoms - Layering and faceting plots - Putting it together

Graphics Matter

Base graphics

hist(df$readSS)
plot of chunk basehist

Base graphics are simple

Base graphics has some limitations

plot(df$readSS, df$mathSS)
plot of chunk basescatter

Basic Plot

library(ggplot2)
qplot(readSS, mathSS, data = df)
plot of chunk plot1

Variations

qplot(readSS, mathSS, data = df) + geom_smooth()
plot of chunk plot2
qplot(readSS, mathSS, data = df, alpha = I(0.3))
plot of chunk plot2
qplot(readSS, mathSS, data = df) + xlab("Reading Score") + ylab("Math Score")
plot of chunk plot2
qplot(readSS, mathSS, data = df, color = race) + scale_color_brewer(type = "qual", 
    palette = 2)
plot of chunk plot2

Visualization Philosophy

Our data for this exercise

A few pro tips

Draw a Colorwheel to show this problem

colwheel <- "https://dl.dropbox.com/u/1811289/colorwheel.R"
dropbox_source(colwheel)
col.wheel("magenta", nearby = 2)
plot of chunk colorwheel
##  [1] "plum"        "violet"      "darkmagenta" "magenta4"    "magenta3"   
##  [6] "magenta2"    "magenta"     "magenta1"    "orchid4"     "orchid"     
col.wheel("orange", nearby = 2)
plot of chunk colorwheel
##  [1] "salmon1"       "darksalmon"    "orangered4"    "orangered3"   
##  [5] "coral"         "orangered2"    "orangered"     "orangered1"   
##  [9] "lightsalmon2"  "lightsalmon"   "peru"          "tan3"         
## [13] "darkorange2"   "darkorange4"   "darkorange3"   "darkorange1"  
## [17] "linen"         "bisque3"       "bisque1"       "bisque2"      
## [21] "darkorange"    "antiquewhite3" "antiquewhite1" "papayawhip"   
## [25] "moccasin"      "orange2"       "orange"        "orange1"      
## [29] "orange4"       "wheat4"        "orange3"       "wheat"        
## [33] "oldlace"      
col.wheel("brown", nearby = 2)
plot of chunk colorwheel
##  [1] "snow1"       "snow2"       "rosybrown"   "rosybrown1"  "rosybrown2" 
##  [6] "rosybrown3"  "rosybrown4"  "lightcoral"  "indianred"   "indianred1" 
## [11] "indianred3"  "brown"       "brown4"      "brown1"      "brown3"     
## [16] "brown2"      "firebrick"   "firebrick1"  "chocolate"   "chocolate4" 
## [21] "saddlebrown" "seashell3"   "seashell2"   "seashell4"   "sandybrown" 
## [26] "peachpuff2"  "peachpuff3" 

Start with a great example

plot of chunk premier

Scary R Code

library(grid)
p1 <- qplot(readSS, ..density.., data = df, fill = race, position = "fill", 
    geom = "density") + scale_fill_brewer(type = "qual", palette = 2)

p2 <- qplot(readSS, ..fill.., data = df, fill = race, position = "fill", geom = "density") + 
    scale_fill_brewer(type = "qual", palette = 2) + ylim(c(0, 1)) + theme_bw() + 
    opts(legend.position = "none", axis.text.x = theme_blank(), axis.text.y = theme_blank(), 
        axis.ticks = theme_blank(), panel.margin = unit(0, "lines")) + ylab("") + 
    xlab("")

vp <- viewport(x = unit(0.65, "npc"), y = unit(0.73, "npc"), width = unit(0.2, 
    "npc"), height = unit(0.2, "npc"))
print(p1)
print(p2, vp = vp)

Now, how?

Aesthetics

ggplot(df, aes(x = readSS, y = mathSS)) + geom_point()
plot of chunk extended

Layers

Geoms

- Geoms are the way data is represented, you can think of it like a chart type in another programming language

Scales

Exercises

1. 2.

References

Session Info

It is good to include the session info, e.g. this document is produced with knitr version 0.7. Here is my session info:

print(sessionInfo(), locale = FALSE)
## R version 2.15.1 (2012-06-22)
## Platform: i386-pc-mingw32/i386 (32-bit)
## 
## attached base packages:
## [1] grid      stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] stringr_0.6.1  quantreg_4.81  SparseM_0.96   lmtest_0.9-30 
##  [5] zoo_1.7-7      gridExtra_0.9  ggplot2_0.9.1  hexbin_1.26.0 
##  [9] lattice_0.20-6 mgcv_1.7-19    Cairo_1.5-1    knitr_0.7     
## [13] plyr_1.7.1    
## 
## loaded via a namespace (and not attached):
##  [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2      
##  [4] evaluate_0.4.2     formatR_0.6        labeling_0.1      
##  [7] MASS_7.3-19        Matrix_1.0-6       memoise_0.1       
## [10] munsell_0.3        nlme_3.1-104       proto_0.3-9.2     
## [13] RColorBrewer_1.0-5 reshape2_1.2.1     scales_0.2.1      
## [16] tools_2.15.1      

Attribution and License

Public Domain Mark
This work (R Tutorial for Education, by Jared E. Knowles), in service of the Wisconsin Department of Public Instruction, is free of known copyright restrictions.